package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.Arrays;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import controller.EmployeeController;

import javax.swing.ScrollPaneConstants;

import model.EmpTableModel;
import model.Employee;

import org.jdesktop.swingx.JXTaskPane;

import javax.swing.DefaultComboBoxModel;

public class PanelNhanVien extends JPanel {
	private static final long serialVersionUID = 8325949040647652945L;
	private JTextField txt_id;
	private JTextField txt_name;
	private JTextField txt_ident;
	private JTextField txt_phone;
	private JTextField txt_address;
	private JTextField txt_birthday;
	private JTextField txt_dateBegin;
	private JTextField txt_regency;
	private JTable table;
	private Vector<Object> data;
	private EmployeeController control;
	private EmpTableModel tableModel;
	private Vector<String> columnnames = new Vector<String>(Arrays.asList(
			"STT", "M\u00E3 NV", "T\u00EAn NV", "Ch\u1EE9c v\u1EE5",
			"Ng\u00E0y  l\u00E0m", "\u0110i\u1EC7n tho\u1EA1i"));
	private JXTaskPane taskPane;
	private JComboBox<String> comboBox;

	/**
	 * Create the panel.
	 */
	public PanelNhanVien() {
		this.control = new EmployeeController(this);
		setLayout(new BorderLayout(0, 0));

		JPanel panel_3 = new JPanel();
		add(panel_3, BorderLayout.CENTER);
		panel_3.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"),
				"Danh s\u00E1ch nh\u00E2n vi\u00EAn", TitledBorder.LEADING,
				TitledBorder.TOP, null, new Color(51, 153, 255)));
		panel_3.setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane = new JScrollPane();
		scrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		panel_3.add(scrollPane, BorderLayout.CENTER);

		// TODO table
		table = new JTable();
		table.addMouseListener(control);
		data = new Vector<Object>();
		control.loadData(data);
		scrollPane.setViewportView(table);

		JToolBar toolBar = new JToolBar();
		toolBar.setOrientation(SwingConstants.VERTICAL);
		toolBar.setFloatable(false);
		panel_3.add(toolBar, BorderLayout.EAST);

		JButton button = new JButton("");
		button.setFocusable(false);
		button.setIcon(new ImageIcon(getClass().getResource("/images/new.png")));
		toolBar.add(button);

		JButton button_1 = new JButton("");
		button_1.setFocusable(false);
		button_1.setIcon(new ImageIcon(getClass().getResource(
				"/images/delete.png")));
		toolBar.add(button_1);

		JButton button_2 = new JButton("");
		button_2.setFocusable(false);
		button_2.setIcon(new ImageIcon(getClass().getResource(
				"/images/refresh.png")));
		toolBar.add(button_2);

		taskPane = new JXTaskPane();
		taskPane.setCollapsed(true);
		taskPane.getContentPane().setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		taskPane.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"),
				"Th\u00F4ng tin nh\u00E2n vi\u00EAn", TitledBorder.LEADING,
				TitledBorder.TOP, null, new Color(51, 153, 255)));
		panel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"), }, new RowSpec[] {
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, }));

		JLabel lblNewLabel = new JLabel("Tên NV");
		panel.add(lblNewLabel, "1, 1, left, default");

		// TODO
		txt_name = new JTextField();
		panel.add(txt_name, "3, 1, fill, default");
		txt_name.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("Mã NV");
		panel.add(lblNewLabel_1, "5, 1, left, default");

		txt_id = new JTextField();
		panel.add(txt_id, "7, 1, fill, default");
		txt_id.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("Ngày sinh");
		panel.add(lblNewLabel_2, "1, 3, left, default");

		txt_birthday = new JTextField();
		panel.add(txt_birthday, "3, 3, fill, default");
		txt_birthday.setColumns(10);

		JLabel lblNewLabel_6 = new JLabel("Số CMND");
		panel.add(lblNewLabel_6, "5, 3, left, default");

		txt_ident = new JTextField();
		panel.add(txt_ident, "7, 3, fill, default");
		txt_ident.setColumns(10);

		JLabel lblNewLabel_5 = new JLabel("Gới tính");
		panel.add(lblNewLabel_5, "1, 5, left, default");

		comboBox = new JComboBox<String>();
		comboBox.setModel(new DefaultComboBoxModel<String>(new String[] {
				"Nam", "Nữ" }));
		panel.add(comboBox, "3, 5, fill, default");

		JLabel lblNewLabel_3 = new JLabel("Ngày làm");
		panel.add(lblNewLabel_3, "5, 5, left, default");

		txt_dateBegin = new JTextField();
		panel.add(txt_dateBegin, "7, 5, fill, default");
		txt_dateBegin.setColumns(10);

		JLabel lblNewLabel_8 = new JLabel("Điện thoại");
		panel.add(lblNewLabel_8, "1, 7, left, default");

		txt_phone = new JTextField();
		panel.add(txt_phone, "3, 7, fill, default");
		txt_phone.setColumns(10);

		JLabel lblChcV = new JLabel("Chức vụ");
		panel.add(lblChcV, "5, 7, left, default");

		txt_regency = new JTextField();
		panel.add(txt_regency, "7, 7, fill, default");
		txt_regency.setColumns(10);

		JLabel lblNewLabel_4 = new JLabel("Địa chỉ");
		panel.add(lblNewLabel_4, "1, 9, left, default");

		txt_address = new JTextField();
		panel.add(txt_address, "3, 9, 5, 1, fill, default");
		txt_address.setColumns(10);
		taskPane.setScrollOnExpand(true);
		add(taskPane, BorderLayout.NORTH);
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new PanelNhanVien());
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
	}

	// TODO
	public void setTableModel(Vector<Object> data) {
		if (tableModel == null)
			tableModel = new EmpTableModel(data, columnnames);
		else
			tableModel.setDataVector(data, columnnames);
		table.setModel(tableModel);
		table.getColumnModel().getColumn(0).setMaxWidth(45);
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(1).setMaxWidth(54);
		table.getColumnModel().getColumn(1).setResizable(false);
	}

	public void showDetail() {
		Employee e = (Employee) data.get(table.getSelectedRow());
		txt_name.setText(e.getName());
		txt_id.setText(e.getId());
		txt_birthday.setText(e.getBirthday().toString());
		txt_ident.setText(e.getIdent());
		txt_address.setText(e.getAddress());
		txt_phone.setText(e.getPhone());
		txt_dateBegin.setText(e.getDateBegin().toString());
		txt_regency.setText(e.getRegency());
		if (e.getSex().equalsIgnoreCase("nữ"))
			comboBox.setSelectedIndex(1);
		else
			comboBox.setSelectedIndex(0);
		taskPane.setCollapsed(false);
	}
}
